#!/usr/bin/perl

#This script allows the user to modify his/her password from the command line

#Variables that will hold user input.
  $username = "$ARGV[0]";
  $password = "$ARGV[1]";

#Open passwords.txt
  open($file, "+<users.txt") or die(exit(4));


#Check to see whether username exists
  while ($line = <$file>) {                       # checks all lines in file
     if( $line =~ m/$username/) {          # RegExp checks to see if username exists


#############If the username exists###############

       # Check the validity of the user's entered password
       # Checks each condition for the password
          if ($password =~ /(\W\S[^|])+/){            #At least one !@#$%^&*(){}[]/?=+\-~“'`:;-_<>,.
            if ($password =~ /\d+/){                  #At least one number
              if ($password =~ /[a-z]/){              #At least one lower case letter
                if($password =~ /[A-Z]/){             #At least one upper case letter
                  if($password =~ /.{6,}/){           #At least 6 letters
                   
                        #Updating the user
                        print("Your password is valid and has been switched");

                        #Run Encryption

                        $cryptPassword = $password;
                            $cryptPassword =~ tr/a-uv-zA-UV-Z0-45-9/f-za-eF-ZA-E5-90-4/;
                           
                        #Input New Password
                        $line =~ s/\|(.+)\|/\|$cryptPassword\|/;
                        print $file "$line";

                    ####Bug here, where a the line is appended to the file and not replaced.
                    ####Not able to implement | and %,$
                         
                    #Exit Messages                
                  }else{print "Invalid Password; password must be at least 6 characters long\n"; exit(911);}  
                }else{print "Invalid Password; password must have at least one upper case letter"; exit(911);}
              }else{print "Invalid Password; password must have at least one lower case letter"; exit(911);}
            }else{print "Invalid Password; password must have at least one digit"; exit(911);}
         }else{print "Invalid Password; password must contain one of !@#$%^&*(){}[]/?=+\\-~\"\'`:;-_<>,."; exit(911);}  
       }
  }


 close $file;

exit(0);



                         
                   
                                        
